<meta charset="UTF-8" />
<title>中文&#互转</title>

<textarea id="inputText"></textarea>

<br>
<br>

<textarea id="outputText"></textarea>

<script>
/** @type { HTMLInputElement } */ const inputText = document.querySelector("#inputText");
/** @type { HTMLInputElement } */ const outputText = document.querySelector("#outputText");

const chineseCharRegExp = /([\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b\u4e00-\u9fa5])/g;

inputText.oninput = function() {
    outputText.value = inputText.value.replaceAll(/&#(\d+);/g, (full, code) => {
        return String.fromCharCode(code);
    });
}

outputText.oninput = function() {
    inputText.value = outputText.value.replaceAll(chineseCharRegExp,
        /** @param { string } code */
        (full, code) => {
            return `&#${code.charCodeAt()};`;
        }
    );
}
</script>

<style>
textarea {
    width: 800px;
    height: 400px;
}
</style>